﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using DBEntity;
namespace iver.Controllers
{
    /// <summary>
    /// 用户前台页面请求
    /// </summary>
    public class PageController : ApiController
    {
        /// <summary>
        /// 得到信息类型列表
        /// </summary>
        /// <returns>jsonstr</returns>
        [HttpGet]
        public ResResult InfoTypeList([FromUri]ReqList req)
        {
            iverDB db = new iverDB();
            var list = (from u in db.apInfoType
                        where u.isEnable == true
                        orderby u.orderNum descending
                        select u);
            var arr = list.Skip(req.limit * (req.page - 1)).Take(req.limit);
            int count = list.Count();
            return new ResResult
            {
                success = true,
                result = new
                {
                    count,
                    arr,
                }
            };

        }
        public class ReqListQueryEx : ReqListQuery
        {
            public string title { set; get; }
            public string remark { set; get; }
        }
        /// <summary>
        /// 获取信息列表
        /// </summary>
        /// <param name="req"></param>
        /// <returns>jsonstr</returns>
        [HttpGet]
        public ResResult InfoList([FromUri]ReqListQueryEx req)
        {
            ////System.Threading.Thread.Sleep(5000);
            iverDB db = new iverDB();
            var list = (from u in db.apInfo

                        where
                         (req.id == null ? 1 == 1 : u.infoTypeId == req.id)
                        && (req.title == null ? 1 == 1 : u.title.Contains(req.title))
                        && (req.remark == null ? 1 == 1 : u.remark == req.remark)
                        && u.isEnable == true
                        //orderby u.orderNum,u.id descending
                        select new
                        {
                            u.id,
                            u.infoTypeId,
                            u.createBy,
                            u.title,
                            u.titleSub,
                            //u.body,
                            u.bodySub,
                            u.cover,
                            u.viewNum,
                            u.orderNum,
                            u.createTime,
                            u.updateTime,
                            u.remark,
                        });
            var arr = list.OrderByDescending(p => p.orderNum).ThenByDescending(p => p.id).Skip(req.limit * (req.page - 1)).Take(req.limit);
            //var arr = list.Skip(req.limit * (req.page - 1)).Take(req.limit);

            int count = list.Count();
            return new ResResult
            {
                success = true,
                result = new
                {
                    count,
                    arr,
                }
            };
        }
        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="req"></param>
        /// <returns>jsonstr</returns>
        [HttpGet]
        /// <summary>
        /// 获取信息对象
        /// </summary>
        /// <param name="req">对象ID</param>
        /// <returns>jsonstr</returns>
        public ResResult Info([FromUri] ReqID<int> req)
        {
            if (req.id == 0)
            {
                return new ResResult
                {
                    success = false,
                    rescode = ResCode.FailParams,
                };
            }
            iverDB db = new iverDB();
            apInfo info = db.apInfo.FirstOrDefault(u => u.id == req.id && u.isEnable == true);
            if (info == null)
            {
                return new ResResult
                {
                    success = false,
                    rescode = ResCode.FailParams,
                };
            }

            info.viewNum = info.viewNum + 1;
            db.SaveChanges();

            return new ResResult
            {
                success = true,
                result = info,
            };
        }
        /// <summary>
        /// 获取信息列表
        /// </summary>
        /// <param name="req"></param>
        /// <returns>jsonstr</returns>
        [HttpGet]
        public ResResult InfoListByHot([FromUri]ReqList req)
        {
            iverDB db = new iverDB();
            var list = (from u in db.apInfo
                        where
                        u.isEnable == true
                        orderby u.viewNum descending
                        select new
                        {
                            u.id,
                            u.title,
                            u.createTime,
                            u.orderNum,
                            u.viewNum,
                        });
            var arr = list.Skip(req.limit * (req.page - 1)).Take(req.limit);
            int count = list.Count();
            return new ResResult
            {
                success = true,
                result = new
                {
                    count,
                    arr,
                }
            };
        }
        /// <summary>
        /// 获取信息列表
        /// </summary>
        /// <param name="req"></param>
        /// <returns>jsonstr</returns>
        [HttpGet]
        public ResResult InfoListByNew([FromUri]ReqList req)
        {
            iverDB db = new iverDB();
            var list = (from u in db.apInfo
                        where
                        u.isEnable == true
                        orderby u.createTime descending
                        select new
                        {
                            u.id,
                            u.title,
                            u.createTime,
                            u.orderNum,
                            u.viewNum,
                        });
            var arr = list.Skip(req.limit * (req.page - 1)).Take(req.limit);
            int count = list.Count();
            return new ResResult
            {
                success = true,
                result = new
                {
                    count,
                    arr,
                }
            };
        }
        /// <summary>
        /// 读取config
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpGet]
        public ResResult ReadConfig([FromUri] ReqKeyValuePair req)
        {
            iverDB db = new iverDB();
            apConfig config = db.apConfig.FirstOrDefault(u => u.name == req.name);
            if (config == null)
            {
                return new ResResult
                {
                    success = false,
                    rescode = ResCode.FailParams,
                };
            }
            return new ResResult
            {
                success = true,
                result = config.jsonString,
            };
        }
        /// <summary>
        /// 图片验证码
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public ResResult imgcode([FromBody]ReqImgCode req)
        {
            return Help.imgcode(req);
        }
        public class ReqOnline : apOnline
        {
            /// <summary>
            /// 
            /// </summary>
            public string imgCode;
        }
        /// <summary>
        /// 在线招生
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public ResResult OnlineAdd([FromBody]ReqOnline req)
        {
            if (System.Web.HttpContext.Current.Session["imgcode"] == null)
            {
                return new ResResult()
                {
                    success = false,
                    rescode = ResCode.FailParams,
                    result = "请填写验证码"
                };
            }
            if (System.Web.HttpContext.Current.Session["imgcode"].ToString().ToLower() != req.imgCode.ToLower())
            {
                return new ResResult()
                {
                    success = false,
                    rescode = ResCode.FailParams,
                    result = "验证码错误"
                };
            }
            iverDB db = new iverDB();
            DateTime now = DateTime.Now;
            db.apOnline.Add(new apOnline()
            {
                createTime = now,
                updateTime = now,
                isEnable = false,
                trueName = req.trueName,
                phoneCode = req.phoneCode,
                temp1 = req.temp1,
                temp2 = req.temp2,
                temp3 = req.temp3,
                temp4 = req.temp4,
                temp5 = req.temp5,
                remark = req.remark,

            });
            db.SaveChanges();
            return new ResResult
            {
                success = true,
                rescode = ResCode.SuccessAdd,
            };
        }
        /// <summary>
        /// 在线招生
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public ResResult OnlineAddEdit([FromBody]ReqOnline req)
        {
            if (System.Web.HttpContext.Current.Session["imgcode"] == null)
            {
                return new ResResult()
                {
                    success = false,
                    rescode = ResCode.FailParams,
                    result = "请填写验证码"
                };
            }
            if (System.Web.HttpContext.Current.Session["imgcode"].ToString().ToLower() != req.imgCode.ToLower())
            {
                return new ResResult()
                {
                    success = false,
                    rescode = ResCode.FailParams,
                    result = "验证码错误"
                };
            }
            iverDB db = new iverDB();
            apOnline model = db.apOnline.FirstOrDefault(u => u.trueName == req.trueName && u.phoneCode == req.phoneCode);
            DateTime now = DateTime.Now;
            if (model == null)
            {
                //添加
                db.apOnline.Add(new apOnline()
                {
                    createTime = now,
                    updateTime = now,
                    isEnable = false,
                    trueName = req.trueName,
                    phoneCode = req.phoneCode,
                    temp1 = req.temp1,
                    temp2 = req.temp2,
                    temp3 = req.temp3,
                    temp4 = req.temp4,
                    temp5 = req.temp5,
                    remark = req.remark,

                });
                db.SaveChanges();
                return new ResResult
                {
                    success = true,
                    rescode = ResCode.SuccessAdd,
                };
            }
            else
            {
                model.createTime = now;
                model.temp1 = req.temp1 == null ? model.temp1 : req.temp1;
                model.temp2 = req.temp2 == null ? model.temp2 : req.temp2;
                model.temp3 = req.temp3 == null ? model.temp3 : req.temp3;
                int result = db.SaveChanges();
                return new ResResult
                {
                    success = true,
                    rescode = ResCode.SuccessEdit,
                };
            }


        }
        /// <summary>
        /// 在线招生-查询结果
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public ResResult OnlineQuery([FromBody]apOnline req)
        {
            iverDB db = new iverDB();
            apOnline model = db.apOnline.FirstOrDefault(u => u.trueName == req.trueName && u.phoneCode == req.phoneCode);
            if (model == null)
            {
                return new ResResult
                {
                    success = false,
                    rescode = ResCode.FailParams,
                };
            }
            return new ResResult
            {
                success = true,
                result = model,
            };
        }

        /// <summary>
        /// 留言板-列表
        /// </summary>
        /// <returns>jsonstr</returns>
        [HttpPost]
        public ResResult GuestbookList([FromBody]ReqList req)
        {
            iverDB db = new iverDB();
            var list = (from u in db.apGuestbook
                        where u.isEnable == true
                        select u);
            var arr = list.OrderByDescending(p => p.orderNum).ThenByDescending(p => p.id).Skip(req.limit * (req.page - 1)).Take(req.limit);
            int count = list.Count();
            return new ResResult
            {
                success = true,
                result = new
                {
                    count,
                    arr,
                }
            };
        }
        public class ReqGuestbook : apGuestbook
        {
            /// <summary>
            /// 
            /// </summary>
            public string imgCode;
        }
        /// <summary>
        /// 留言板-发布
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public ResResult GuestbookAdd([FromBody]ReqGuestbook req)
        {
            if (System.Web.HttpContext.Current.Session["imgcode"] == null)
            {
                return new ResResult()
                {
                    success = false,
                    rescode = ResCode.FailParams,
                    result = "请填写验证码"
                };
            }
            if (System.Web.HttpContext.Current.Session["imgcode"].ToString().ToLower() != req.imgCode.ToLower())
            {
                return new ResResult()
                {
                    success = false,
                    rescode = ResCode.FailParams,
                    result = "验证码错误"
                };
            }
            iverDB db = new iverDB();
            DateTime now = DateTime.Now;
            db.apGuestbook.Add(new apGuestbook()
            {
                nickName = req.nickName,
                message = req.message,
                createTime = now,
                updateTime = now,
                isEnable = false,
                orderNum = 0,
            });
            db.SaveChanges();
            return new ResResult
            {
                success = true,
                rescode = ResCode.SuccessAdd,
            };
        }
        [HttpPost]
        public ResResult SetHot([FromBody]ReqID<int> req)
        {
            iverDB db = new iverDB();
            apInfo model = db.apInfo.FirstOrDefault(u => u.id == req.id);
            if (model == null)
            {
                return new ResResult
                {
                    success = false,
                    rescode = ResCode.FailParams,
                };
            }
            model.viewNum = model.viewNum + 1;
            int result = db.SaveChanges();
            return new ResResult
            {
                success = true,
                rescode = ResCode.SuccessEdit,
                result = new
                {
                    viewNum = model.viewNum,
                },
            };
        }
    }
}